package com.steak.account.execute;

import com.steak.common.domain.dataobject.tcc.Order;
import com.steak.common.domain.dto.AccountDTO;
import com.steak.common.result.Response;
import lombok.AllArgsConstructor;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

/**
 * @author 刘牌
 * @Title:
 * @date 2022/3/721:40
 */
@Component
@AllArgsConstructor
public class AccountExecute {

    final JdbcTemplate jdbcTemplate;

    public Response execute(AccountDTO accountDTO) {
        String sql = "UPDATE account SET balance = ( SELECT accou.balance FROM ( SELECT ( acc.balance - "+accountDTO.getMoney()+" ) balance FROM account acc WHERE acc.id = "+accountDTO.getUserId()+" ) accou ) , freeze_balance = "+accountDTO.getMoney()+" WHERE id = "+accountDTO.getUserId()+"";
        jdbcTemplate.execute(sql);
        return new Response(200,"decrease balance success",null);
    }

    public Response updateAccountBalanceToZeroById(Integer userId) {
        String sql = "update account SET freeze_balance = 0 WHERE id = "+userId+"";
        jdbcTemplate.update(sql);
        return new Response(200,"update balance to zero success",null);
    }

    public Response updateAccountBalance(AccountDTO accountDTO) {
        String sql = "UPDATE account SET balance = ( SELECT accou.balance FROM ( SELECT ( acc.balance + "+accountDTO.getMoney()+" ) balance FROM account acc WHERE acc.id = "+accountDTO.getUserId()+" ) accou ) , freeze_balance =0 WHERE id = "+accountDTO.getUserId()+"";
        jdbcTemplate.execute(sql);
        return new Response(200,"recovery balance success",null);
    }
}
